//
//  ThreadViewController.swift
//  SwiftDemo
//
//  Created by Jason on 15/6/1.
//  Copyright (c) 2015年 Jason Jiang. All rights reserved.
//

import UIKit

class ThreadViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        
        self.testNSThread()
        self.dlImgWithOperationQueue()
        self.dlImgWithDispatch()
        // Do any additional setup after loading the view.
        
        
        let btn = UIButton (frame: CGRectMake(0, 400, 250, 40));
        btn.backgroundColor = UIColor.redColor()
        btn .setTitle("jumpToBlockAndDependence", forState: UIControlState.Normal)
        btn.addTarget(self, action: "jumpAction:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(btn)
    }

    
    func jumpAction(btn:UIButton){
        
        let dvc = ThreadDependenceViewController ()
        self.navigationController?.pushViewController(dvc, animated: true)
        
        
    }
    
    //fuction as below
    
    
    //======================NSThread Method
    
    func testNSThread(){
        
        NSThread.detachNewThreadSelector("dlBigImage", toTarget: self, withObject: nil);
        
    }
    
    
    func dlBigImage(){
        
        
        let data = NSData (contentsOfURL: NSURL (string: "http://e.hiphotos.baidu.com/image/pic/item/908fa0ec08fa513dea2d3b7e3e6d55fbb3fbd98f.jpg")!)
        
        let img = UIImage (data: data!)

        if let _ = img{
            
         NSLog("i am in subThread 0 NSThread %@",   [NSThread .isMainThread()])
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                
                NSLog("i am in subThread 1 NSThread %@",   [NSThread .isMainThread()])
                self.showImgV(img!)

            })
            
        }
        
    }
    
    
    func showImgV(img : UIImage){
        
    
        let imgV = UIImageView (image: img)
        imgV.frame = CGRectMake(0, 64, CGRectGetWidth(self.view.frame), 300);
        self.view.addSubview(imgV)
        
    }
    
    //========================NSOperator queue
    
    func dlImgWithOperationQueue(){
        
    let queue = NSOperationQueue ()
    
    let myOp = myOperationThread ()
        myOp.delegate = self
        
    queue.addOperation(myOp)
        
        
    }
    
    
    class myOperationThread: NSOperation {
        
        weak var delegate:ThreadViewController?
        
        override func start() {
            
            super.start()
        }
        
        override func main() {
            
            let data = NSData (contentsOfURL: NSURL (string: "http://d.hiphotos.baidu.com/image/pic/item/fc1f4134970a304ee6536555d2c8a786c9175c28.jpg")!)
            
            let img = UIImage (data: data!)
            
            if let _ = img{
                
                NSLog("i am in subThread 0 NSOperation %@",   [NSThread .isMainThread()])
                
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    
                    NSLog("i am in subThread 1 NSOperation%@",   [NSThread .isMainThread()])
                    self.delegate!.showImgV(img!)
                    
                    
                })
                
            }
            
        }
    }
    
    
    //===========================================
    //===========================================
    
    
    
    func dlImgWithDispatch(){
        

        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in
            
            let data = NSData (contentsOfURL: NSURL (string: "http://g.hiphotos.baidu.com/image/pic/item/359b033b5bb5c9ea7337d84cd639b6003bf3b35d.jpg")!)
            
            let img = UIImage (data: data!)
            
            if let imgT = img{
                
                NSLog("i am in subThread 0 GCD %@",   [NSThread .isMainThread()])
                
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    
                    NSLog("i am in subThread 1 GCD%@",   [NSThread .isMainThread()])
                    self.showImgV(img!)
                    

                })
            }
        })
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    

    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
